#include "randomGenerator.h"

#define seq(X,Y,Z,T) (char)(T<<6 | Z<<4 | Y<<2 | X)
/* MEMO
 * A 0
 * C 1
 * G 2
 * T 3
 */

map<int, int> count(size_t listSize, DnaSeq * list)
{
	map<int,int> m;
	string s;
	for_each(list, list + listSize, 
		[&s, &m](DnaSeq seq)
		{
			s="";
			conv2string(seq, s);
			for(const char c : s)
			{
				switch(c)
				{
					case 'A' : m[0]++; break;
					case 'C' : m[1]++; break;
					case 'G' : m[2]++; break;
					case 'T' : m[3]++; break;
					default : cerr << "bad character" << endl;
				}
			}
		}
	);
		
	return m;
}
 
vector<int> generateList(unsigned int nb, float rateA, float rateC, float rateG, float rateT)
{
    vector<int> res;	
    
    random_device rd;
    mt19937 gen(rd());
    discrete_distribution<> d({rateA, rateC, rateG, rateT});
    
    for(int n=0; n<nb; ++n)
		res.push_back(d(gen));
		
	return res;
}

DnaSeq * createList(size_t listSize, unsigned int trimming, float rateA, float rateC, float rateG, float rateT)
{
	DnaSeq * list = new DnaSeq[listSize];
	DnaSeq * tmp = new DnaSeq;
	unsigned int tabsize = (trimming%4) ? trimming/4+1 : trimming/4; //6 chars = 24 nucléotides
	char tab[tabsize]; 
	
	vector<int> nucl = generateList(listSize * trimming, rateA, rateC, rateG, rateT);

	int index = 0;
	for (unsigned int i=0; i<listSize; i++)
	{			
		// création de la DnaSeq	
		for (unsigned int j=0; j<tabsize; j+=1)
		{
			tab[j] = seq(nucl[index], nucl[index+1], nucl[index+2], nucl[index+3]);
			index +=4;
		}
		fillDnaSeq(tmp, 0, trimming, tab);
		list[i] = *tmp;
	}
	
	return list;
}
